package com.neuedu.yunong.service.impl;
import com.neuedu.yunong.domain.YunongFamilyMemberInfo;
import lombok.RequiredArgsConstructor;
import java.util.List;
import java.util.Random;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neuedu.yunong.mapper.YunongHouseholdInfoMapper;
import com.neuedu.yunong.domain.YunongHouseholdInfo;
import com.neuedu.yunong.service.IYunongHouseholdInfoService;
import com.neuedu.yunong.mapper.YunongFamilyMemberInfoMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 户主信息Service业务层处理
 *
 * @author zl
 * @date 2024-12-10
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class YunongHouseholdInfoServiceImpl extends ServiceImpl<YunongHouseholdInfoMapper, YunongHouseholdInfo> implements IYunongHouseholdInfoService
{
    private final YunongHouseholdInfoMapper yunongHouseholdInfoMapper;

    @Autowired
    private YunongFamilyMemberInfoMapper yunongFamilyMemberInfoMapper;

    /**
     * 查询户主信息
     *
     * @param id 户主信息主键
     * @return 户主信息
     */
    @Override
    public YunongHouseholdInfo selectYunongHouseholdInfoById(Long id)
    {
        return yunongHouseholdInfoMapper.selectYunongHouseholdInfoById(id);
    }

    /**
     * 查询户主信息列表
     *
     * @param yunongHouseholdInfo 户主信息
     * @return 户主信息
     */
    @Override
    public List<YunongHouseholdInfo> selectYunongHouseholdInfoList(YunongHouseholdInfo yunongHouseholdInfo)
    {
        return yunongHouseholdInfoMapper.selectYunongHouseholdInfoList(yunongHouseholdInfo);
    }

    /**
     * 新增户主信息
     *
     * @param yunongHouseholdInfo 户主信息
     * @return 结果
     */
    @Override
    public int insertYunongHouseholdInfo(YunongHouseholdInfo yunongHouseholdInfo)
    {
        return yunongHouseholdInfoMapper.insertYunongHouseholdInfo(yunongHouseholdInfo);
    }

    /**
     * 修改户主信息
     *
     * @param yunongHouseholdInfo 户主信息
     * @return 结果
     */
    @Override
    public int updateYunongHouseholdInfo(YunongHouseholdInfo yunongHouseholdInfo)
    {
        return yunongHouseholdInfoMapper.updateYunongHouseholdInfo(yunongHouseholdInfo);
    }

    /**
     * 批量删除户主信息
     *
     * @param ids 需要删除的户主信息主键
     * @return 结果
     */
    @Override
    public int deleteYunongHouseholdInfoByIds(Long[] ids)
    {
        return yunongHouseholdInfoMapper.deleteYunongHouseholdInfoByIds(ids);
    }

    /**
     * 删除户主信息信息
     *
     * @param id 户主信息主键
     * @return 结果
     */
    @Override
    public int deleteYunongHouseholdInfoById(Long id)
    {
        return yunongHouseholdInfoMapper.deleteYunongHouseholdInfoById(id);
    }


    @Override
    @Transactional
    public int saveHouseholdWithMembers(YunongHouseholdInfo householdInfo, List<YunongFamilyMemberInfo> familyMembers) {
        // 生成 household_id
        householdInfo.setHouseholdId(generateHouseholdId());
        // 插入户主信息
        yunongHouseholdInfoMapper.insertYunongHouseholdInfo(householdInfo);

        // 获取生成的 household_id
        Long householdId = yunongHouseholdInfoMapper.getHouseholdId();

        // 绑定 household_id 给家庭成员
        for (YunongFamilyMemberInfo member : familyMembers) {
            member.setHouseholdId(householdId);
        }

        System.out.println("this family" + familyMembers);
        // 批量插入家庭成员信息
        if (!familyMembers.isEmpty()) {

            yunongFamilyMemberInfoMapper.insertFamilyMemberInfoBatch(familyMembers);
        }
        return  1;
    }

    private Long generateHouseholdId() {
        return System.currentTimeMillis() * 1_000_000 + new Random().nextInt(1_000_000); //乘法随机数防止同一毫秒冲突，理论来讲可以保证一毫秒内100万次插入不重复
    }
}
